package com.arnold.learn.job;

import com.arnold.learn.couchbase.CouchbaseUtil;
import com.arnold.learn.impala.ImpalaUtil;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by arnold.zhu on 2017/7/31.
 */
public class InvokeStatJobDetail {

    private static Logger logger = LoggerFactory.getLogger(InvokeStatJobDetail.class);

    private InvokeStatJobDetail() {
    }

    // 当天交易成功笔数
    public static void addToday_number(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TODAY_NUMBER);
        JsonArray jsonArray = JsonArray.from();

        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> Tranum: {}", resultSet.getInt(1));

                JsonObject data = JsonObject.create()
                        .put("Tranum", resultSet.getInt(1));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error("Error!!!!");
        }

        JsonObject tempData = JsonObject.create().put("today_number", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "today_number", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 当天交易成功金额
    public static void addToday_amount(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TODAY_AMOUNT);
        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> Amount: {}", resultSet.getDouble(1));

                JsonObject data = JsonObject.create()
                        .put("Amount", resultSet.getDouble(1));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        JsonObject tempData = JsonObject.create().put("today_amount", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "today_amount", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 当天销售金额趋势
    public static void addDay_sales_trends(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TODAY_SALES_TRENDS);
        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> Amount: {}, Hour: {}", resultSet.getDouble(1), resultSet.getInt(2));
                JsonObject data = JsonObject.create()
                        .put("Amount", resultSet.getDouble(1))
                        .put("Hour", resultSet.getInt(2));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JsonObject tempData = JsonObject.create().put("today_sales_trends", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "today_sales_trends", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 近30天销售金额趋势
    public static void addLatest_30_day_sales_trends(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.LATEST_30_DAY_SALES_TRENDS);

        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> Amount: {}, Date: {}", resultSet.getDouble(1), resultSet.getString(2));
                JsonObject data = JsonObject.create()
                        .put("Amount", resultSet.getDouble(1))
                        .put("Date", resultSet.getString(2));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JsonObject tempData = JsonObject.create().put("latest_30_day_sales_trends", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "latest_30_day_sales_trends", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 当天错误率最高的十个店
    public static void addTop_10_error_shops(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TOP_10_ERROR_SHOPS);
        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> ErrorCount: {}, StoreNumber: {}", resultSet.getInt(1), resultSet.getString(2));
                JsonObject data = JsonObject.create()
                        .put("ErrorCount", resultSet.getInt(1))
                        .put("StoreNumber", resultSet.getString(2));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JsonObject tempData = JsonObject.create().put("top_10_error_shops", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "top_10_error_shops", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 当天错误率最高的十种操作
    public static void addTop_10_number_of_exceptions(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TOP_10_NUMBER_OF_EXCEPTIONS);
        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> ErrorCount: {}, Type: {}, ErrorCode: {}",
                        resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3));

                JsonObject data = JsonObject.create()
                        .put("ErrorCount", resultSet.getInt(1))
                        .put("Type", resultSet.getString(2))
                        .put("ErrorCode", resultSet.getString(3));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JsonObject tempData = JsonObject.create().put("top_10_number_of_exceptions", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "top_10_number_of_exceptions", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 当天支付方式（成功）统计
    public static void addToday_payment_method(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TODAY_PAYMENT_METHOD);
        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> Count: {}, Type: {}",
                        resultSet.getInt(1), resultSet.getString(2));

                JsonObject data = JsonObject.create()
                        .put("Count", resultSet.getInt(1))
                        .put("Type", resultSet.getString(2));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JsonObject tempData = JsonObject.create().put("today_payment_method", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "today_payment_method", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }

    // 当天地区交易成功统计
    public static void addToday_area(Connection connection, Bucket bucket) {
        ResultSet resultSet = ImpalaUtil.getQueryResult(connection, InvokeStatSQL.TODAY_AREA);
        JsonArray jsonArray = JsonArray.from();
        try {
            while (resultSet.next()) {
                logger.info(">>>>>>>>>>>>>>>>>> Count: {}, City: {}",
                        resultSet.getInt(1), resultSet.getString(2));

                JsonObject data = JsonObject.create()
                        .put("Count", resultSet.getInt(1))
                        .put("City", resultSet.getString(2));
                jsonArray.add(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JsonObject tempData = JsonObject.create().put("today_area", jsonArray);

        CouchbaseUtil.upsertDocument(bucket, "today_area", tempData);
        ImpalaUtil.closeResultSet(resultSet);
    }
}
